{extend name="index/public" /}

{block name="content"}
<form class="layui-form" action="" id="searchBox">
    <div class="layui-inline">
        <select name="channel_id" id="channel_id" lay-filter="channel_id" lay-search="">
            <option value="">选择渠道（全部）</option>
            {volist name="$channel_data" id="vo"}
            <option value="{$vo.cps_id}">{$vo.cps_id}：{$vo.cps_name}</option>
            {/volist}
        </select>
    </div>
</form>

<table id="tableBox" lay-filter="tableBox"></table>

<script type="text/html" id="checkboxTpl">
    <input type="checkbox" name="cps_week" value="{{d.cps_id}}" title="周结" lay-filter="cps_week" {{ d.cps_week == 1 ? 'checked' : '' }}>
</script>
{/block}

{block name="css"}
{/block}

{block name="js"}
<script>
    layui.use('table', function(){
        var table = layui.table
            ,$ = layui.$
            ,form = layui.form
            ,load = null
            ,active = {
                reload: function () {
                    var channel_id = $('#channel_id').val();

                    table.reload('tableId', {
                        page: {curr: 1}
                        ,where: {channel_id:channel_id}
                        ,height: 'full-100'
                    })
                },
            };

        //表格初始化
        table.render({
            elem: '#tableBox'
            ,height: 'full-100'
            ,url: '{:url("AuditCpsInfo/index")}'
            ,page: true
            ,limit:20
            ,limits:[20,40,60,80]
            ,loading:true
            ,id:'tableId'
            ,done: function (res, curr, count) {
                layer.close(load);
                $('#layui-loading').hide();
            }
            ,cols: [[
                {field:'cps_id', title: '渠道ID', width: 90, sort:true}
                ,{field:'cps_name', title: '渠道名称' , width: 160}
                ,{field:'cps_type', title: '渠道类型' , width: 90}
                ,{field:'pay_rate', title: '分成比例', width: 90, edit: 'text'}
                ,{field:'channel_rate', title: '渠道费率' , width: 90, edit: 'text'}
                ,{field:'tax_rate', title: '税率' , width: 80, edit: 'text'}
                ,{field:'contact_man', title: '收款人' , width: 120, edit: 'text'}
                ,{field:'open_account', title: '银行账号' , width: 220, edit: 'text'}
                ,{field:'open_name', title: '所属银行' , width: 220, edit: 'text'}
                ,{field:'open_bank', title: '开户银行' , width: 220, edit: 'text'}
                ,{field:'audit_time', title: '最后转账时间' , width: 150}
                ,{field:'cps_week', title: '是否周结', templet: '#checkboxTpl'}
            ]]
        });

        //监听搜索下拉菜单
        form.on('select(channel_id)', function(data){
            load = layer.load();
            active['reload'].call(this)
        });

        //修改进游提示
        form.on('checkbox(cps_week)', function(obj){
            var cps_week = obj.elem.checked ? 1 : 0
                ,load = layer.load();
            $.post('{:url("AuditCpsInfo/edit")}',{cps_id:obj.value, cps_week:cps_week}, function(json){
                layer.close(load);
                if(json.code==0){
                    layer.msg(json.msg, {icon: 1, time:1000})
                }else{
                    layer.msg(json.msg, {icon: 0, time:1000});
                }
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
        });

        //监听单元格编辑
        table.on('edit(tableBox)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field
                ,params = {}
                ,load = layer.load();

            if(field=='pay_rate'){
                params = {cps_id:data.cps_id, pay_rate:value};
            }else if(field=='channel_rate'){
                params = {cps_id:data.cps_id, channel_rate:value};
            }else if(field=='tax_rate'){
                params = {cps_id:data.cps_id, tax_rate:value};
            }else if(field=='contact_man'){
                params = {cps_id:data.cps_id, contact_man:value};
            }else if(field=='open_account'){
                params = {cps_id:data.cps_id, open_account:value};
            }else if(field=='open_name'){
                params = {cps_id:data.cps_id, open_name:value};
            }else if(field=='open_bank'){
                params = {cps_id:data.cps_id, open_bank:value};
            }

            $.post('{:url("AuditCpsInfo/edit")}',params, function(json){
                layer.close(load);
                if(json.code==0){
                    layer.msg(json.msg, {icon: 1, time:1000})
                }else{
                    layer.msg(json.msg, {icon: 0, time:1000});
                }
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
        });
    });
</script>
{/block}